package com.ruoyi.web.controller.mes;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.domain.mes.MesCustom;
import com.ruoyi.web.domain.mes.MesMaterialClass;
import com.ruoyi.web.domain.mes.MesPaper;
import com.ruoyi.web.domain.vo.*;
import com.ruoyi.web.service.IMesMaterialClassService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.mes.MesMaterial;
import com.ruoyi.web.service.IMesMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料管理Controller
 *
 * @author levicash
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/mes/material")
public class MesMaterialController extends BaseController {
    @Autowired
    private IMesMaterialService mesMaterialService;

    @Autowired
    private IMesMaterialClassService mesMaterialClassService;

    /**
     * 查询物料管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MesMaterial mesMaterial) {
        startPage();
        List<MesMaterial> list = mesMaterialService.selectMesMaterialList(mesMaterial);
        return getDataTable(list);
    }


    /**
     * 导出物料管理列表
     */
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesMaterial mesMaterial) {
        List<MesMaterial> list = mesMaterialService.selectMesMaterialList(mesMaterial);
        ExcelUtil<MesMaterial> util = new ExcelUtil<MesMaterial>(MesMaterial.class);
        util.exportExcel(response, list, "物料管理数据");
    }

    /**
     * 获取物料管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mesMaterialService.selectMesMaterialById(id));
    }

    /**
     * 新增物料管理
     */
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesMaterial mesMaterial) {
        MesMaterialClass materialClass = new MesMaterialClass();
        materialClass.setCode(mesMaterial.getBelongClass());
        List<MesMaterialClass> classList = mesMaterialClassService.selectMesMaterialClassList(materialClass);
        if (classList.size() > 0) {
            MesMaterialClass materialClassHasChild = new MesMaterialClass();
            materialClassHasChild.setPid(classList.get(0).getId());
            List<MesMaterialClass> classListHasChild = mesMaterialClassService.selectMesMaterialClassList(materialClassHasChild);
            // 分类项不是子节点不允许添加物料信息
            if (classList.get(0).getPid() == 0 && classListHasChild.size() > 0) {
                return AjaxResult.error("该分类项包含子节点暂不支持添加物料信息");
            }

        }

        return toAjax(mesMaterialService.insertMesMaterial(mesMaterial));
    }

    /**
     * 修改物料管理
     */
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesMaterial mesMaterial) {
        MesMaterial mode = mesMaterialService.selectMesMaterialById(mesMaterial.getId());
        if (mode.getStatus() == 0) {
            return AjaxResult.warn("请先停用后再修改");
        }
        return toAjax(mesMaterialService.updateMesMaterial(mesMaterial));
    }

    /**
     * 删除物料管理
     */
    @Log(title = "物料管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<MesMaterial> mesMaterials = mesMaterialService.selectMesMaterialByIds(ids);
        Integer num = 0;
        for (MesMaterial item : mesMaterials) {
            if (item.getStatus() == 0) {
                num++;
            }
        }
        if (num > 0) {
            return AjaxResult.warn("包含未停用状态项，请先停用后再删除");
        }
        return toAjax(mesMaterialService.deleteMesMaterialByIds(ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MesMaterial mesMaterial) {
        return toAjax(mesMaterialService.updateMesMaterialStatus(mesMaterial));
    }

    /**
     * 查询物料一览
     */
    @GetMapping("/browse")
    public List<MaterialBrowseVo> browse(MaterialBrowseVo materialBrowseVo) {
        // startPage();
        Map<String, Object> map = new HashMap<>();
//        map.put("materialClass", materialBrowseVo.getMaterialClass());
//        map.put("materialCode", materialBrowseVo.getMaterialCode());
//        map.put("materialName", materialBrowseVo.getMaterialName());
//        map.put("specs", materialBrowseVo.getSpecs());
        String materialClass = materialBrowseVo.getMaterialClass() == null ? "" : materialBrowseVo.getMaterialClass();
        map.put("materialClass", materialClass); //materialBrowseVo.getMaterialClass()
        String materialCode = materialBrowseVo.getMaterialCode() == null ? "" : materialBrowseVo.getMaterialCode();
        map.put("materialCode", materialCode);// materialBrowseVo.getMaterialCode()
        String materialName = materialBrowseVo.getMaterialName() == null ? "" : materialBrowseVo.getMaterialName();
        map.put("materialName", materialName);//materialBrowseVo.getMaterialName()
        String specs = materialBrowseVo.getSpecs() == null ? "" : materialBrowseVo.getSpecs();
        map.put("specs", specs);//materialBrowseVo.getSpecs()
        map.put("beginTime", materialBrowseVo.getBeginTime());
        map.put("endTime", materialBrowseVo.getEndTime());
        List<MaterialBrowseVo> list = mesMaterialService.selectMesMaterialBrowse(map);
        return list;
    }

    /**
     * 查询物料明细列表
     */
    @GetMapping("/detail")
    public TableDataInfo detail(MaterialVo materialVo) {
        startPage();
        Map<String, Object> map = new HashMap<>();
        map.put("materialCode", materialVo.getMaterialCode());
        map.put("materialName", materialVo.getMaterialName());
        List<MaterialVo> list = mesMaterialService.selectMesMaterialListDetail(map);
        return getDataTable(list);
    }

    /**
     * 查询物料明细列表
     */
    @GetMapping("/machine")
    public TableDataInfo machine(MaterialReturnVo materialReturnVo) {
        startPage();
        Map<String, Object> map = new HashMap<>();
        map.put("deptId", materialReturnVo.getDeptId());
        map.put("machineId", materialReturnVo.getMachineId());
        map.put("materialCode", materialReturnVo.getMaterialCode());
        map.put("materialName", materialReturnVo.getMaterialName());
        List<MaterialVo> list = mesMaterialService.selectMaterialMachine(map);
        return getDataTable(list);
    }

    /**
     * 查询物料领用明细列表  20240325 added  原因：不能和退库中机台领用明细共用一个方法
     */
    @GetMapping("/machineDetail")
    public TableDataInfo machineDetail(MaterialUseVo materialUseVo) {
        startPage();
        Map<String, Object> map = new HashMap<>();
        map.put("deptId", materialUseVo.getDeptId());
        map.put("machineName", materialUseVo.getMachineName());
        map.put("useCode", materialUseVo.getUseCode());
        map.put("materialName", materialUseVo.getMaterialName());
        map.put("status", materialUseVo.getStatus());
        map.put("beginTime", materialUseVo.getBeginTime());
        map.put("endTime", materialUseVo.getEndTime());
        List<MaterialUseVo> list = mesMaterialService.selectMaterialMachineDetail(map);
        return getDataTable(list);
    }
}
